package com.kyzh.core.activities

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.atools.util.AView
import com.gushenge.atools.util.setMargins
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.PointSmallProduct
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemPointsmallBinding
import com.kyzh.core.utils.dip
import com.kyzh.core.utils.onLoginClick
import com.kyzh.core.utils.startActivity
import kotlinx.android.synthetic.main.activity_pointsmall_v3.*

class PointsMallActivity : BaseActivity(){

    private val  context = this@PointsMallActivity
    private val beans = arrayListOf<PointSmallProduct>()
    private val adapter = Adapter(R.layout.item_pointsmall,beans)
    private var p =1
    private var max =2

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pointsmall_v3)
        initView()
    }

    private fun initView() {
        AView.setStatusBar(context,false)
        ivClose.setMargins(dip(4), AView.getStatusBarHeight(context),0,0)
        ivClose.setOnClickListener { finish() }
        tvMyProduct.onLoginClick(context) {
            startActivity<MyOrderActivity>()
        }
        rvPointSmall.layoutManager = GridLayoutManager(this,2)
        rvPointSmall.adapter = adapter
        initData()
    }

    private fun initData() {
        refresh()
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=false
        adapter.loadMoreModule.setOnLoadMoreListener {
            if (max<p){
                adapter.loadMoreModule.loadMoreEnd()
            }else{
                getData()
                adapter.loadMoreModule.loadMoreComplete()
            }
        }
        adapter.setOnItemClickListener { adapter, view, position ->
            context.startActivity<ProductDetailActivity>(GlobalKeys.KEY to beans[position].goods_id)

        }
    }


    private inner class Adapter(layout:Int,beans:ArrayList<PointSmallProduct>):BaseQuickAdapter<PointSmallProduct,BaseViewHolder>(layout,beans),LoadMoreModule{

        override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
            DataBindingUtil.bind<ItemPointsmallBinding>(viewHolder.itemView)
        }
        override fun convert(holder: BaseViewHolder, item: PointSmallProduct) {
            holder.getBinding<ItemPointsmallBinding>()?.let {
                it.data = item
            }
        }
    }


    fun getData(){
        WealRequest.pointSmall(p){ point,p,max_p,data->
            this.p = p
            this.max = max_p
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
    fun refresh(){
        WealRequest.pointSmall(1){point,p,max_p,data->
            this.p = p
            this.max = max_p
            beans.clear()
            tvPoint.text = point
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
}
